package com.ming.reggie.service.impl;

import com.ming.reggie.entity.*;
import com.ming.reggie.mapper.*;
import com.ming.reggie.service.OrderService;
import com.ming.reggie.service.ShoppingCartService;
import com.ming.reggie.util.UUIDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired(required = false)
    private AddressBookMapper addressBookMapper;

    @Autowired(required = false)
    private UserMapper userMapper;

    @Autowired(required = false)
    private OrderMapper orderMapper;

    @Autowired(required = false)
    private OrderDetailMapper orderDetailMapper;

    @Autowired(required = false)
    private ShoppingCartMapper shoppingCartMapper;
    /**
     * 新增订单的业务方法
     *
     * @param orders
     * @param userId
     */
    @Override
    @Transactional
    public void addOrder(Orders orders, Long userId) {
        //orders 已经封装了: remark,addressBookId,payMethod
        //1.根据传递过来的收货地址id获取地址对象
        AddressBook addressBook = addressBookMapper.getById(orders.getAddressBookId());

        //2.根据userId获取购物车对象
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.findAllByUserId(userId);


        User user = userMapper.findById(userId);

        //完善订单对象Orders数据
        Long oid = UUIDUtils.getUUIDInOrderId().longValue();
        orders.setId(oid);
        orders.setNumber(oid.toString());
        orders.setStatus(1);
        orders.setUserId(userId);
        orders.setOrderTime(LocalDateTime.now());
        orders.setCheckoutTime(LocalDateTime.now());
        orders.setPhone(addressBook.getPhone());
        orders.setAddress(addressBook.getDetail());
        //用户的姓名
        orders.setUserName(user.getName());
        //收件人
        orders.setConsignee(addressBook.getConsignee());

        //完善订单明细对象List<OrderDetail>数据
        List<OrderDetail> orderDetailList = new ArrayList<>();
        BigDecimal total = new BigDecimal("0");
//        遍历购物车集合数据封装每个orderDetailList对象
        for (ShoppingCart shoppingCart : shoppingCartList) {
            //将每个购物商品数据shoppingCart封装到OrderDetail对象中
            OrderDetail orderDetail = new OrderDetail();
            long orderDetailId = UUIDUtils.getUUIDInOrderId().longValue();
            orderDetail.setId(orderDetailId);
            //菜品或套餐名
            orderDetail.setName(shoppingCart.getName());
            orderDetail.setImage(shoppingCart.getImage());
            orderDetail.setOrderId(orders.getId());
            orderDetail.setDishId(shoppingCart.getDishId());
            orderDetail.setSetmealId(shoppingCart.getSetmealId());
            orderDetail.setDishFlavor(shoppingCart.getDishFlavor());
            orderDetail.setNumber(shoppingCart.getNumber());
            orderDetail.setAmount(shoppingCart.getAmount());

            //将封装好的orderDetail对象加入到list中
            orderDetailList.add(orderDetail);
            total = total.add(orderDetail.getAmount().multiply(new BigDecimal(orderDetail.getNumber()+"")));
        //注意:完善订单主表的总价 number*amount
        }

        orders.setAmount(total);

        //调用mapper新增订单主表数据
        orderMapper.save(orders);
        //调用mapper新增订单从表数据
        orderDetailMapper.saveBatch(orderDetailList);
        //下单成功后,删除购物车数据
        shoppingCartMapper.deleteByUserId(userId);

    }
}
